﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 第五次作业第二题
{
    public partial class Form1 : Form
    {
        
        public Form1()
        {
            InitializeComponent();
        }
        ArrayList list = new ArrayList();
        /*生成随机数按钮触发事件*/
        private void generate_Click(object sender, EventArgs e)
        {
            try
            {
                textBox1.Text += "\r\n生成100个随机数：\r\n";//windows窗体输出换行用\r\n
                Random random = new Random();//随机数定义在循环体外面，可以避免生成相同随机数的问题
                list.Clear();
                for (int i = 0; i < 100; i++)
                {
                    list.Add(random.Next(1000));
                }
                foreach (var item in list)
                {
                    textBox1.Text += item.ToString() + "\t";
                }
            }
            catch (Exception) { }
        }
        /*排序按钮触发事件*/
        private void sort_Click(object sender, EventArgs e)
        {
            try
            {
                textBox1.Text += "\r\n排序后的随机数：\r\n";//windows窗体输出换行用\r\n
                var l = from n in list.OfType<int>()
                        orderby n descending
                        select n;
                foreach (var item in l)
                {
                    textBox1.Text += item.ToString() + "\t";
                }
            }
            catch (Exception){ }
        }
        /*求和与平均数按钮触发事件*/
        private void calculate_Click(object sender, EventArgs e)
        {
            try
            {
                int sum=0;
                double ave;
                foreach (var item in list)
                {
                    sum += Convert.ToInt32(item);
                }
                ave = 1.0*sum / list.Count;
                textBox1.Text += "\r\n随机数之和：";
                textBox1.Text += sum.ToString();
                textBox1.Text += "\r\n随机数求平均：";
                textBox1.Text += ave.ToString();
            }
            catch (Exception){ }
        }
    }
}
